package com.yeyuanqi.web.admin;

import com.github.pagehelper.PageInfo;
import com.yeyuanqi.model.ResponseData.ArticleResponseData;
import com.yeyuanqi.model.ResponseData.StaticticsBo;
import com.yeyuanqi.model.domain.Article;
import com.yeyuanqi.model.domain.Comment;
import com.yeyuanqi.model.domain.CommentCount;
import com.yeyuanqi.model.domain.User;
import com.yeyuanqi.service.IArticleService;
import com.yeyuanqi.service.ICommentService;
import com.yeyuanqi.service.ISiteService;
import com.yeyuanqi.service.IUserService;
import com.yeyuanqi.vo.CountVO;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/admin")
public class AdminController {
    private static final Logger logger = LoggerFactory.getLogger(AdminController.class);

    @Autowired
    private ISiteService siteServiceImpl;

    @Autowired
    private IArticleService articleServiceImpl;

    @Autowired
    private ICommentService commentServiceImpl;

    @Autowired
    private IUserService userServiceImpl;


    //管理中心起始页
    @GetMapping(value = {"","/index"})
    public String index(HttpServletRequest request){
//        获取前五章博客、评论、统计数据
        List<Article> articles = siteServiceImpl.recentArticles(5);
        List<Comment> comments = siteServiceImpl.recentComments(5);
        StaticticsBo staticticsBo = siteServiceImpl.getStatistics();

        request.setAttribute("comments",comments);
        request.setAttribute("articles",articles);
        request.setAttribute("statistics",staticticsBo);
        return "back/index";
    }

    @GetMapping(value = "/article/toEditPage")
    public String newArticle(){
        return "back/article_edit";
    }

    //发表文章
    @PostMapping(value = "/article/publish")
    @ResponseBody
    public ArticleResponseData publishArticle(Article article){
        if (StringUtils.isBlank(article.getCategories())) {
            article.setCategories("默认分类");
        }
        try {
            articleServiceImpl.publish(article);
            logger.info("文章发布成功");
            return ArticleResponseData.ok();
        } catch (Exception e) {
            logger.info("文章发布失败,错误信息:"+e.getMessage());
            return ArticleResponseData.fail();
        }
    }

    //跳转到后台文章列表页面
    @GetMapping(value = "/article")
    public String index(@RequestParam(value = "page",defaultValue = "1") int page,
                        @RequestParam(value = "count",defaultValue = "5") int count,
                        HttpServletRequest request){
        PageInfo<Article> pageInfo = articleServiceImpl.selectArticleWithPage(page,count);
        request.setAttribute("articles",pageInfo);
        return "back/article_list";
    }

    //向文章修改页面跳转
    @GetMapping(value = "/article/{id}")
    public String editArticle(@PathVariable("id") String id,HttpServletRequest request){
        Article article = articleServiceImpl.selectArticleWithId(Integer.parseInt(id));
        request.setAttribute("contents",article);
        request.setAttribute("categories",article.getCategories());
        return "back/article_edit";
    }

    //跳转到评论列表页面
    @GetMapping(value = "/comments")
    public String index2(@RequestParam(value = "page",defaultValue = "1") int page,
                         @RequestParam(value = "count",defaultValue = "5") int count,
                         HttpServletRequest request){
        PageInfo<Comment> pageInfo = commentServiceImpl.getAllComment(page,count);
        request.setAttribute("comments",pageInfo);
        return "back/commons_list";
    }

    //修改文章
    @PostMapping(value = "/article/modify")
    @ResponseBody
    public ArticleResponseData modiftArticle(Article article){
        try {
            articleServiceImpl.updateArticleWithId(article);
            logger.info("文章更新成功");
            return ArticleResponseData.ok();
        } catch (Exception e){
            logger.info("文章更新失败,错误信息:"+e.getMessage());
            return ArticleResponseData.fail();
        }
    }

    //跳转到标签列表页面
    @GetMapping(value = "/tag")
    public String index3(@RequestParam(value = "page",defaultValue = "1") int page,
                         @RequestParam(value = "count",defaultValue = "5") int count,
                         HttpServletRequest request){
        PageInfo<Article> pageInfo = articleServiceImpl.selectArticleAll(page,count);
        request.setAttribute("tag",pageInfo);
        return "back/tags_list";
    }

    //删除文章
    @PostMapping(value = "/article/delete")
    @ResponseBody
    public ArticleResponseData delete(@RequestParam int id){
        try {
            articleServiceImpl.deleteArticleWithId(id);
            logger.info("文章删除成功");
            return ArticleResponseData.ok();
        } catch (Exception e){
            logger.info("文章删除失败,错误信息:"+e.getMessage());
            return ArticleResponseData.fail();
        }
    }

    @PostMapping(value = "/comment/delete")
    @ResponseBody
    public ArticleResponseData deleteComment(@RequestParam int id){
        try {
            commentServiceImpl.delCommentById(id);
            logger.info("评论删除成功");
            return ArticleResponseData.ok();
        } catch (Exception e){
            logger.info("评论删除失败,错误信息:"+e.getMessage());
            return ArticleResponseData.fail();
        }
    }

    @PostMapping(value = "/tag/delete")
    @ResponseBody
    public ArticleResponseData deleteTag(@RequestParam int id){
        try {
            articleServiceImpl.deleteTagsWithId(id);
            logger.info("标签删除成功");
            return ArticleResponseData.ok();
        } catch (Exception e){
            logger.info("标签删除失败,错误信息:"+e.getMessage());
            return ArticleResponseData.fail();
        }
    }

    //跳转评论统计页面
    @GetMapping(value = "/comment/count")
    public String countComment(HttpServletRequest request){
//        CountVO countVO = commentServiceImpl.selectCommentCount();
//        request.setAttribute("countVO",countVO);
        return "back/comment_count";
    }


    //跳转到用户列表页面
    @GetMapping(value = "/user")
    public String user(@RequestParam(value = "page",defaultValue = "1") int page,
                       @RequestParam(value = "count",defaultValue = "5") int count,
                       HttpServletRequest request){
        PageInfo<User> pageInfo = userServiceImpl.selectUserWithPage(page,count);
        request.setAttribute("user",pageInfo);
        return "back/user_list";
    }

    //添加用户
    @PostMapping(value = "/user/add")
    @ResponseBody
    public ArticleResponseData addUser(User user){

        try {
            userServiceImpl.publishUser(user) ;
            logger.info("用户添加成功");
            return ArticleResponseData.ok();
        } catch (Exception e) {
            logger.info("用户添加失败,错误信息:"+e.getMessage());
            return ArticleResponseData.fail();
        }
    }

    //删除用户
    @PostMapping(value = "/user/delete")
    @ResponseBody
    public ArticleResponseData deleteUser(@RequestParam int id){
        try {
            userServiceImpl.deleteUserWithId(id);
            logger.info("用户删除成功");
            return ArticleResponseData.ok();
        } catch (Exception e){
            logger.info("用户删除失败,错误信息:"+e.getMessage());
            return ArticleResponseData.fail();
        }
    }

}
